<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    .curtabtr th{background-color: #fafafa;height: 40px;color:#909399;}
    .curtabtr td{background-color: #fafafa;height: 40px}
</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm" style="padding-bottom: 500px">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <?php include "../public/substt.php";?>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=zhi_lianjie','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=zhi_lianjie','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目资金信息</span></div>
                </div>
            </div>
            <div class="layui-form-item height_auto" style="padding: 0 26px!important;">
                <table class="layui-table" lay-size="sm">
                    <thead>
                    <tr class="curtabtr">
                        <th colspan="3" style="text-align: center">项目收入</th>
                        <th colspan="3" style="text-align: center">项目支出</th>
                        <th rowspan="2">费用扣收(元)</th>
                        <th rowspan="2">资金余额(元)</th>
                        <th rowspan="2">待扣管理费(元)</th>
                        <th rowspan="2">待扣税费(元)</th>
                        <th rowspan="2">待扣其他费(元)</th>
                    </tr>
                    <tr class="curtabtr">
                        <th>工程回款金额(元)</th>
                        <th>联营方备用金(元)</th>
                        <th>收入合计(元) A</th>
                        <th>项目付款金额(元)</th>
                        <th>联营结算金额(元)</th>
                        <th>支出合计(元) B</th>
                    </tr>
                    </thead>
                    <tbody class="short_table_tbody" style="min-height: unset!important;">
                    <tr class="curtabtr">
                        <td><input type="text" id="shougonghuifee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="shoulianbeifee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="lianyingshoufee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="zhixiangfee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="zhilianjiefee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="lianyingzhifee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="kou_allfee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="balancefee2" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="dai_guanfee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="dai_shuifee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                        <td><input type="text" id="daikouqidaifee" readonly autocomplete="off" class="layui-input moneyinput disabled"></td>
                    </tr>
                    </tbody>
                </table>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>资金核算</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">项目资金余额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="balancefee" id="balancefee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>本次结算是否扣收</label>
                        <div class="layui-input-block">
                            <select id="iskou" name="iskou" lay-filter="iskou">
                                <option value="">请选择</option>
                                <option value="是">是</option>
                                <option value="否">否</option>
                            </select>
                            <p class="error" id="iskou_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item iskoushi layui-hide">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">本次扣收合计(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="koufee" id="koufee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其中管理费扣收(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="kouguanfee" id="kouguanfee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item iskoushi layui-hide">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其中税费扣收(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="koushuifee" id="koushuifee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">其中其他费扣收(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="kouqifee" id="kouqifee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">本次结算净额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="jingfee" id="jingfee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>结算日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="jiedt" id="jiedt" placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">本次结算后管理费待扣(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="daiguanfee" id="daiguanfee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">本次结算后税费待扣(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="daishuifee" id="daishuifee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">本次结算后其他费待扣(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="daiqifee" id="daiqifee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto iskoushi layui-hide">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>扣收明细</span></div>
                        <div>
                            <button type="button" onclick="choose_dai_kou_qi_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">从其他费待扣登记选择</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>来源</th>
                            <th><text>*</text>费用项</th>
                            <th><text>*</text>扣收金额(元)</th>
                            <th>款项说明</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>结算支付信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>结算单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">款项类型</label>
                        <div class="layui-input-block">
                            <input type="text" id="feilei" value="其他费" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">费用项</label>
                        <div class="layui-input-block">
                            <input type="text" id="feilei" value="联营结算" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>款项摘要</label>
                        <div class="layui-input-block">
                            <input type="text" name="zhai" id="zhai" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>本次结算支付金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>支付方式</label>
                        <div class="layui-input-block">
                            <div id="incdaotype"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>收款方</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="duier" class="layui-input duier" onclick="openPage('../public/choose_dealcom.php?dom=duier&page=zhi_lianjie','80%','80%')">
                            <div class="choose_div" id="duier" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_dealcom.php?dom=duier&page=zhi_lianjie','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>账户账号</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="banknum" class="layui-input banknum" onclick="choose_dealcom_bank()">
                            <div class="choose_div" id="banknum" key="1"></div>
                            <span class="choose_one" onclick="choose_dealcom_bank()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开户行</label>
                        <div class="layui-input-block">
                            <input type="text" name="bankname" id="bankname" readonly autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <?php include '../public/flowqita.php'?>
            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>

<?php include 'showrrboxtext.php'?>
<div><?php include '../public/shrrbox_addpage.php'?></div>
<script>
    $("#commentForm").validate({
        rules: {
            jiedt: {required: true},
            zhai: {required: true},
        },
        messages:{
            jiedt: {required: "请选择"},
            zhai: {required: "请输入"},
        }
    });

    function choose_dealcom_bank(obj){
        var domkey = $(obj).parents('tr').index();
        var name = get_choose_val('duier');
        if(!name){
            facade.error("请先选择收款方");return false;
        }
        openPage('../public/choose_dealcom_bank.php?page=zhi_lianjie&name='+encodeURIComponent(name)+'&domkey='+domkey,'80%','80%');
    }

    function choose_dai_kou_qi_list() {
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目名称");return false;
        }
        openPage('../public/choose_dai_kou_qi_list.php?page=zhi_lianjie&enginid='+enginid,'80%','80%');
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_add_zhi_lianjie}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                chu_flow_qian(data);

                $("#number").val(data.number);

                xmselect_dan('incdaotype', res.data.incdaotype, '', function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#incdaotype").find('xm-select').removeClass('error');
                    }else{
                        $("#incdaotype").find('xm-select').addClass('error');
                    }
                });


                var kou_feitype = data.kou_feitype[0].children;
                var kouAddStr = '';
                kouAddStr += '<tr>';
                kouAddStr += '<td style="width: 45px">1</td>';
                kouAddStr += '<td><input type="text" class="layui-input daikouqiid disabled" key="0" value="/" disabled></td>';
                kouAddStr += '<td><select class="layui-select feitype" lay-filter="kou_feilei">';
                kouAddStr += '<option value="">请选择</option>';
                for (var mmm in kou_feitype) {
                    kouAddStr += '<option value="'+kou_feitype[mmm].id+'">'+kou_feitype[mmm].name+'</option>';
                }
                kouAddStr += '</select></td>';
                kouAddStr += '<td><input type="number" class="layui-input money"></td>';
                kouAddStr += '<td><input type="text" class="layui-input explain"></td>';
                kouAddStr += '</tr>';
                short_table_opt_func(kouAddStr, 1);


                public_upload_file_load('upload_files', res.data.files);
                render_from();
                initParseMoney();
            }
        });

        form.on('select(kou_feilei)', function(data){
            jisuan_koufee_all();
        });

        form.on('select(iskou)', function(data){
            if(data.value == '是') {
                $(".iskoushi").removeClass('layui-hide');
            }else{
                $(".iskoushi").addClass('layui-hide');
            }
            jisuan_koufee_all();
            if(data.value == ''){
                $('#iskou_error').show().text('请选择');
                $('#iskou_error').parent().find('input').addClass('select_error');
            }else{
                $('#iskou_error').hide();
                $('#iskou_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            parseMoney(data);

            data.field.enginid = get_choose_val('enginid');
            data.field.duier = get_choose_val('duier');
            data.field.banknum = get_choose_val('banknum');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                formValid('duier');
                if(data.field.duier == undefined || !data.field.duier){
                    validRes = false;
                }

                if(!data.field.incdaotype){
                    $("#incdaotype").find('xm-select').addClass('error');
                    validRes = false;
                }else{
                    $("#incdaotype").find('xm-select').removeClass('error');
                }

                formValid('banknum');
                if(data.field.banknum == undefined || !data.field.banknum){
                    validRes = false;
                }

                if(!validRes){return false;}
            }


            var kou = [''];
            if(data.field.iskou == '是'){
                try {
                    $('#list_list tr').each(function (i) {
                        var daikouqiid = $(this).find('.daikouqiid').attr('key') || 0;
                        if(daikouqiid > 0){
                            var feitype = $(this).find('.feitype').attr('key') || 0;
                        }else{
                            var feitype = $(this).find('.feitype').val();
                        }
                        var money = $(this).find('.money').val();
                        var explain = $(this).find('.explain').val();

                        var index = i + 1;
                        if(data.field.isturn > 0){
                            if(!feitype){
                                throw new Error('第'+index+'行，请选择费用项');
                            }
                            if(money==''){
                                throw new Error('第'+index+'行，请输入扣收金额');
                            }

                            if(daikouqiid > 0){
                                var chongKey = check_chongfu(i, daikouqiid);
                                if(chongKey){
                                    throw new Error('第'+chongKey+'行'+'与第'+index+'行，数据重复');
                                }
                            }
                        }

                        kou.push({
                            daikouqiid:daikouqiid,
                            feitype:feitype,
                            money:money,
                            explain:explain,
                        });
                    });
                } catch(e) {
                    $(".listerror").find('span').text(e.message);
                    $(".listerror").show();
                    return false;
                }
                $(".listerror").hide();
                data.field.kou = kou;
            }


            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_zhi_lianjie, ask_flowdata_edit_zhi_lianjie, $(this).attr('but'));
            return false;
        });

    });

    //检测重复
    function check_chongfu(index, daikouqiid){
        try {
            $('#list_list tr').each(function (i) {
                var cur_daikouqiid = $(this).find('.daikouqiid').attr('key') || 0;
                if(index != i){
                    if(daikouqiid == cur_daikouqiid){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }


    function jisuan_money_all() {
        var iskou = $("#iskou").val() || '';
        var balancefee = parseValidNum($("#balancefee").val() || 0) || 0;
        var koufee = parseValidNum($("#koufee").val() || 0) || 0;
        if(iskou != '是'){
            koufee = 0;
        }

        var jingfee = parseFloat(balancefee) - parseFloat(koufee);
        $("#jingfee").val(jingfee);
        $("#money").val(jingfee);

        var dai_guanfee = parseValidNum($("#dai_guanfee").val() || 0) || 0;
        var kouguanfee = parseValidNum($("#kouguanfee").val() || 0) || 0;
        var daiguanfee = parseFloat(dai_guanfee) - parseFloat(kouguanfee);
        $("#daiguanfee").val(daiguanfee);

        var dai_shuifee = parseValidNum($("#dai_shuifee").val() || 0) || 0;
        var koushuifee = parseValidNum($("#koushuifee").val() || 0) || 0;
        var daishuifee = parseFloat(dai_shuifee) - parseFloat(koushuifee);
        $("#daishuifee").val(daishuifee);

        var daikouqidaifee = parseValidNum($("#daikouqidaifee").val() || 0) || 0;
        var kouqifee = parseValidNum($("#kouqifee").val() || 0) || 0;
        var daiqifee = parseFloat(daikouqidaifee) - parseFloat(kouqifee);
        $("#daiqifee").val(daiqifee);

        // initParseMoney();
    }


    $('#list_list').on('input propertychange','.money',function () {
        jisuan_koufee_all();
    });
    function jisuan_koufee_all() {
        var allkoufee = 0;
        var allkouguanfee = 0;
        var allkoushuifee = 0;
        var allkouqifee = 0;

        $('#list_list tr').each(function (i) {
            var money = parseFloat($(this).find('.money').val()) || 0;
            var feitype = $(this).find('.feitype option:selected').text();
            if(feitype == '管理费'){
                allkouguanfee = parseFloat(allkouguanfee) + parseFloat(money);
            }else if(feitype == '税费'){
                allkoushuifee = parseFloat(allkoushuifee) + parseFloat(money);
            }else{
                allkouqifee = parseFloat(allkouqifee) + parseFloat(money);
            }
            allkoufee = parseFloat(allkoufee) + parseFloat(money);
        });
        $("#koufee").val(allkoufee);
        $("#kouguanfee").val(allkouguanfee);
        $("#koushuifee").val(allkoushuifee);
        $("#kouqifee").val(allkouqifee);

        jisuan_money_all();
    }

    function update_refresh_row_func() {
        render_date();
        jisuan_koufee_all();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }


</script>
</body>
</html>
